package lu.med.praxiswahl.wahlumed.client.arztdatabase;

import java.awt.Component;

import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

import lu.med.praxiswahl.wahlumed.client.utilities.KillInterface;


/**
 * 
 * @author Lars Holger Roth
 */
@SuppressWarnings("serial")
public class ArztCellEditor extends AbstractCellEditor implements
	TableCellEditor, KillInterface
{
    private Component component;
    private ArztTableModel model;
    private String val = "";

    public ArztCellEditor(ArztTableModel model)
    {
	this.model = model;
	model.setKiller(this);
    }

    public Object getCellEditorValue()
    {
	if (component.getClass() == JCheckBox.class)
	{
	    JCheckBox jcb = (JCheckBox) component;
	    return jcb.isSelected();
	}
	else
	{
	    JTextField jtf = (JTextField) component;
	    return jtf.getText();
	}
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
	    boolean isSelected, int rowIndex, int colIndex)
    {
	val = model.getValue(rowIndex, colIndex);
	if (colIndex == 999)
	{
	    JCheckBox jcb = new JCheckBox();
	    if (val.equals("true"))
		jcb.setSelected(true);
	    component = jcb;
	}
	else
	{
	    JTextField jtf = new JTextField();
	    jtf.setText(val);
	    // jtf.setComponentPopupMenu(new MEG08FlexiblePopup(model));
	    //jtf.selectAll();
	    component = jtf;
	}
	return component;
    }

    public void killEditing()
    {
	if (this.component.getClass() == JCheckBox.class)
	{
	    JCheckBox jcb = (JCheckBox) component;
	    model.setValueAt(jcb.isSelected(), model.getSelected(), 8);
	}
	this.cancelCellEditing();
    }

}
